package com.example;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DbTestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");

        String jdbcURL = "jdbc:mysql://localhost:3306/webapp";
        String dbUsername = "root";
        String dbPassword = "Imzdc6455!";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(jdbcURL, dbUsername, dbPassword);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT 'Database connected!' AS message");

            while (rs.next()) {
                out.println("<h2>" + rs.getString("message") + "</h2>");
            }
        } catch (Exception e) {
            out.println("<h2>Database connection failed: " + e.getMessage() + "</h2>");
        }

        out.println("</body></html>");
    }
}
